# 智能猜数游戏与学习分析器
import random
import time

print("=" * 50)
print("          智能猜数游戏与学习分析器")
print("=" * 50)
print(" ---------游戏规则:")
print("1. 我会想一个1-100之间的数字")
print("2. 你尝试猜出这个数字")
print("3. 每次猜完后我会告诉你猜大了还是猜小了")
print("4. 游戏会记录你的表现并进行分析")
print("=" * 50)

# 初始化游戏数据
games_played = 0
total_attempts = 0
best_score = float('inf')  # 初始化为无穷大
all_attempts = []  # 存储每轮游戏的尝试次数

# 主游戏循环
while True:
    games_played += 1
    print(f"\n--------第 {games_played} 轮游戏开始!")
    
    # 生成随机数字
    secret_number = random.randint(1, 100)
    attempts = 0
    guess_history = []  # 记录本轮的猜测历史
    
    # 单轮游戏循环
    while True:
        try:
            guess = int(input("请输入你的猜测 (1-100): "))
            if guess < 1 or guess > 100:
                print("❌ 请输入1-100之间的数字!")
                continue
        except ValueError:
            print("❌ 请输入有效的数字!")
            continue
        
        attempts += 1
        guess_history.append(guess)
        
        # 判断猜测结果
        if guess == secret_number:
            print(f"****** 恭喜你！你在第{attempts}次尝试时猜对了!")
            break
        elif guess < secret_number:
            print("❌ 猜小了，再试一次!")
        else:
            print("❌ 猜大了，再试一次!")
    
    # 更新统计数据
    total_attempts += attempts
    all_attempts.append(attempts)
    
    # 更新最佳成绩
    if attempts < best_score:
        best_score = attempts
        print(f"****** 新纪录！你的最佳成绩是 {best_score} 次尝试!")
    
    # 显示本轮猜测历史
    print("\n###### 本轮猜测历史:", end=" ")
    for i, g in enumerate(guess_history):
        if g < secret_number:
            print(f"{g}(小)", end=" → " if i < len(guess_history)-1 else "")
        elif g > secret_number:
            print(f"{g}(大)", end=" → " if i < len(guess_history)-1 else "")
        else:
            print(f"{g}(正确!)", end="")
    print()
    
    # 询问是否再玩一局
    play_again = input("\n还想再玩一局吗? (输入y继续，其他键退出): ")
    if play_again.lower() != 'y':
        break

# 游戏结束后的统计分析
print("\n" + "=" * 50)
print("          游戏统计分析")
print("=" * 50)

# 基础统计
print(f"总游戏轮数: {games_played}")
print(f"总尝试次数: {total_attempts}")
print(f"平均每次游戏尝试次数: {total_attempts/games_played:.2f}")
print(f"最佳成绩: {best_score} 次尝试")

# 使用循环分析尝试次数的分布
print("\n###### 尝试次数分布:")
attempts_count = {}
for attempts in all_attempts:
    if attempts in attempts_count:
        attempts_count[attempts] += 1
    else:
        attempts_count[attempts] = 1

# 显示分布
for count in sorted(attempts_count.keys()):
    print(f"{count}次尝试: {attempts_count[count]}轮游戏")

# 找出最常见的尝试次数
most_common = max(attempts_count, key=attempts_count.get)
print(f"\n最常见的尝试次数: {most_common}次 (出现了{attempts_count[most_common]}次)")

# 学习进度分析
print("\n###### 学习进度分析:")
if games_played > 1:
    improvement = all_attempts[0] - all_attempts[-1]
    if improvement > 0:
        print(f"你的表现提高了 {improvement} 次尝试!")
    elif improvement < 0:
        print(f"你的表现下降了 {-improvement} 次尝试!")
    else:
        print("你的表现保持稳定!")
        
    # 计算进步趋势
    total_improvement = 0
    for i in range(1, len(all_attempts)):
        improvement = all_attempts[i-1] - all_attempts[i]
        total_improvement += improvement
    
    avg_improvement = total_improvement / (len(all_attempts) - 1)
    if avg_improvement > 0:
        print(f"平均每轮进步: {avg_improvement:.2f} 次尝试")
    else:
        print("需要更多练习来提升技巧!")

# 提供学习建议
print("\n###### 学习建议:")
if total_attempts/games_played > 7:
    print("尝试使用二分查找策略：总是猜中间值")
    print("例如：先猜50，如果小了就猜75，如果大了就猜25")
elif total_attempts/games_played > 5:
    print("不错的表现！尝试减少不必要的猜测")
    print("注意观察模式，数字通常在特定范围内")
else:
    print("太棒了！你已经掌握了高效的猜测策略")

print("\n###### 感谢使用智能猜数游戏与学习分析器!")
